#from CustomGuiItems.MyDockWidget import MyDockWidget;
from PyQt4 import QtGui, QtCore;

class MazeStInput(QtCore.QObject):    
    def __init__(self, objData):
        super(MazeStInput, self).__init__();
        self.dataModel = objData.dataModel;
        self.objData = objData;
        self.parent = objData.parent;
        self.graphScene = objData.graphScene;
        self.mainWin = objData.parent.mainWin;
#        self.myDockWidget = MyDockWidget("Maze State Type", self.parent);
#        self.mainWin.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.myDockWidget);
        self.createDockContent();
#        self.myDockWidget.setWidget(self.qWidget);
        self.initWallsArray();
        
    def initWallsArray(self):
        self.aWalls = [];
        self.aWalls.append([True, False, False, False]); #1
        self.aWalls.append([False, True, False, False]); #2
        self.aWalls.append([False, False, True, False]); #3
        self.aWalls.append([False, False, False, True]); #4
        self.aWalls.append([True, True, False, False]); #5
        self.aWalls.append([False, True, True, False]); #6
        self.aWalls.append([False, False, True, True]); #7
        self.aWalls.append([True, False, False, True]); #8
        self.aWalls.append([True, True, True, False]); #9
        self.aWalls.append([False, True, True, True]); #10
        self.aWalls.append([True, False, True, True]); #11
        self.aWalls.append([True, True, False, True]); #12
        self.aWalls.append([True, False, True, False]); #13
        self.aWalls.append([False, True, False, True]); #14
        self.aWalls.append([False, False, False, False]); #15
        self.aWalls.append([True, True, True, True]); #16
        
    []#Update ListBox from DataModel     
    def updateLbx(self):
        nStates = self.dataModel.nStates;
        self.lbxSts.clear();
        for i in range(nStates):
            self.lbxSts.addItem("State " + str(i+1));
                
    def onLbxStsClick(self):
        for i in range(len(self.aChkBtns)):
            self.aChkBtns[i].setChecked(False);
        iRow = self.lbxSts.currentRow();
        iTypeW = int(self.dataModel.stInfo[iRow][0])-1;
        if iTypeW > len(self.aWalls) or iTypeW < 0: return;
        for i in range(len(self.aChkBtns)):
            self.aChkBtns[i].setChecked(self.aWalls[iTypeW][i]);
        
    def onChkBtnClick(self):
        aWalls = [];
        for i in range(len(self.aChkBtns)): #@UnusedVariable
            aWalls.append(self.aChkBtns[i].isChecked());
        iWall = self.aWalls.index(aWalls)+1;
        iRow = self.lbxSts.currentRow();
        self.setTypeWall(iRow, iWall);
        self.lbxSts.setFocus();
    
    def setTypeWall(self, iSt, iWall):
#         self.dataModel.stInfo[iSt][0] = str(iWall);
        self.objData.dataView.gridStInfo.updateGrid();
    
    def onChkMouseEdit(self, bVal):
        self.graphScene.bMazeEdit = bVal;
        
    def createDockContent(self):
        #CheckButtons
        self.aChkBtns = [];
        sP = "Images/TypeWalls/";
        self.aChkBtns.append(QtGui.QPushButton(QtGui.QIcon(sP+'wL.png'), "", self.mainWin)); #Left
        self.aChkBtns.append(QtGui.QPushButton(QtGui.QIcon(sP+'wT.png'), "", self.mainWin)); #Top
        self.aChkBtns.append(QtGui.QPushButton(QtGui.QIcon(sP+'wR.png'), "", self.mainWin)); #Right
        self.aChkBtns.append(QtGui.QPushButton(QtGui.QIcon(sP+'wB.png'), "", self.mainWin)); #Bottom
        for chkBtn in self.aChkBtns:
            chkBtn.setIconSize(QtCore.QSize(30,30));
            chkBtn.setCheckable(True);
#            chkBtn.setAutoExclusive(True);
            chkBtn.setMaximumWidth(36);
            chkBtn.setMinimumHeight(36);
            chkBtn.connect(chkBtn, QtCore.SIGNAL("clicked()"), self.onChkBtnClick);
        self.chkMouseEdit = QtGui.QCheckBox("Edit by Mouse", self.mainWin);
        self.chkMouseEdit.setChecked(True);
        self.chkMouseEdit.connect(self.chkMouseEdit, QtCore.SIGNAL("toggled(bool)"), self.onChkMouseEdit);
        
        #ListBox
        self.lbxSts = QtGui.QListWidget(self.mainWin);
        self.lbxSts.setMaximumWidth(90);
        self.updateLbx();
        self.lbxSts.setCurrentRow(0);
        self.lbxSts.connect(self.lbxSts, QtCore.SIGNAL("itemSelectionChanged()"), self.onLbxStsClick);
        
        self.gridLayout = QtGui.QGridLayout();
        self.gridLayout.setMargin(0);
        self.gridLayout.setSpacing(2);
        self.gridLayout.addWidget(self.lbxSts, 0,0, 3,1);
        self.gridLayout.addWidget(self.aChkBtns[0], 1,1); #Left
        self.gridLayout.addWidget(self.aChkBtns[2], 1,3); #Right
        self.gridLayout.addWidget(self.aChkBtns[1], 0,2); #Top
        self.gridLayout.addWidget(self.aChkBtns[3], 2,2); #Bottom
        self.gridLayout.addWidget(self.chkMouseEdit, 4,0);
#        self.gridLayout.setColumnStretch(2,1); #Bottom
#        iChk = 0;
#        for i in range(4):
#            for j in range(4):
#                if iChk >= len(self.aChkBtns): break;
#                self.gridLayout.addWidget(self.aChkBtns[iChk], i,j+1);
#                iChk += 1;
        self.qWidget = QtGui.QWidget(self.mainWin);
        self.qWidget.setLayout(self.gridLayout);
        self.vBoxMain = QtGui.QVBoxLayout();
        self.vBoxMain.addWidget(self.qWidget); 
                
